﻿using System;
using System.Windows;
using System.Windows.Controls;
using Headsight.BPMN2WF.ConversionWizard;
using Headsight.BPMN2WF.GUI.Designer;

namespace Headsight.BPMN2WF.GUI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        private readonly ViewModel _viewModel;

        public MainWindow()
        {
            InitializeComponent();

            _viewModel = new ViewModel();
            DataContext = _viewModel;
        }

        private void BtnCancel_Click(object sender, RoutedEventArgs e)
        {
            var curApp = Application.Current;
            curApp.Shutdown();
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            var dlg = new Microsoft.Win32.SaveFileDialog
                          {FileName = "Workflow4", DefaultExt = ".xaml", Filter = "Workflow documents (.xaml)|*.xaml"};

            var result = dlg.ShowDialog();

            if (result == true)
            {
                // Save document
                var filename = dlg.FileName;
                _viewModel.WfDesigner.Save(filename);
                MessageBox.Show("Workflow File saved." + Environment.NewLine + "Filename: " + filename, "Workflow saved");
            }
        }

        private void Open_Click(object sender, RoutedEventArgs e)
        {
            var db = new WizardHost {Owner = this};
            var result = db.ShowDialog();

            if (result.HasValue && result.Value)
            {
                LoadDesigner();
            }
        }

        private void LoadDesigner()
        {
            var toolbox = ToolboxUtils.CreateToolboxControl();
            Grid.SetColumn(toolbox, 0);
            DesignerGrid.Children.Add(toolbox);

            _viewModel.WfDesigner = WorkflowDesignerUtils.CreateWorkflowDesigner(BPMN2WFViewModel.xamlTempFile);
            var view = _viewModel.WfDesigner.View;
            Grid.SetColumn(view, 1);
            DesignerGrid.Children.Add(view);

            var propInspector = _viewModel.WfDesigner.PropertyInspectorView;
            Grid.SetColumn(propInspector, 2);
            DesignerGrid.Children.Add(propInspector);

            _viewModel.DesignerVisible = true;
        }
    }
}
